<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
        <a-col :span="6">
          <a-form-item label="车辆编号">
          <a-input placeholder="请输入车辆编号" v-model="queryParam.carCode"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="6" >
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">{{$t('common.query')}}</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">{{$t('common.reset')}}</a-button>
              
            </span>
          </a-col>
       </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">{{$t('common.table.add')}}</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('故障处理')">{{$t('common.table.export')}}</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">{{$t('common.table.import')}}</a-button>
      </a-upload>
      <!-- 高级查询区域 -->
      <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>{{$t('common.table.delete')}}</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px">{{$t('common.table.batchActions')}}<a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i>{{$t('common.table.selected')}}<a style="font-weight: 600">{{ selectedRowKeys.length }}</a>{{$t('common.table.item')}}
        <a style="margin-left: 24px" @click="onClearSelected">{{$t('common.table.empty')}}</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="assignHandler(record)">指派处理人</a>
       

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">{{$t('common.table.more')}} <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="reminder(record)">催单</a>
              </a-menu-item>
              <a-menu-item>
                <a @click="handleEdit(record)">{{$t('common.table.edit')}}</a>
              </a-menu-item>
              <a-menu-item>
                <a @click="handleDetail(record)">{{$t('common.table.detail')}}</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm :title="$t('common.table.confirmDelete')" @confirm="() => handleDelete(record.id)">
                  <a>{{$t('common.table.delete')}}</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <rental-fault-handle-modal ref="modalForm" @ok="modalFormOk"></rental-fault-handle-modal>


    <a-modal
      title="指定故障处理人"
      width="80%"
      :visible="assignDLVisible"
      @ok="handleAssignDLOk"
      @cancel="handleAssignDLCancel"
    >
     <a-form-model ref="form" :model="assignDLModel" :rules="assignDLRules">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="故障对象" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="objTypeName">
              <a-input readonly v-model="assignDLModel.objTypeName" placeholder="请输入故障对象id" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="设备编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="objCode">
              <a-input readonly v-model="assignDLModel.objCode" placeholder="请输入设备编号" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="经度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="longitude">
              <a-input readonly v-model="assignDLModel.longitude" placeholder="请输入经度"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="维度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="latitude">
              <a-input readonly v-model="assignDLModel.latitude" placeholder="请输入维度"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="故障名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="faultCodeName">
              <a-input readonly v-model="assignDLModel.faultCode" placeholder="请输入故障名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="故障状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="faultStatusName">
             <a-input readonly v-model="assignDLModel.faultStatusName" placeholder="请输入故障状态"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="故障描述" :labelCol="maxLabelCol" :wrapperCol="maxWrapperCol" prop="faultDesc">
              <a-textarea readonly v-model="assignDLModel.faultDesc" rows="4" placeholder="请输入故障描述" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="上报人手机号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="phone">
              <a-input readonly v-model="assignDLModel.phone" placeholder="请输入维度"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="故障上报人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="faultReporter">
              <a-input readonly v-model="assignDLModel.faultReporter" placeholder="请输入故障上报人"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="上报时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="reportedDate" >
              <j-date readOnly placeholder="请选择上报时间" v-model="assignDLModel.reportedDate" date-format="YYYY-MM-DD HH:mm:ss"  style="width: 100%" />
            </a-form-model-item>
          </a-col>

          <a-col :span="12">
            <a-form-model-item label="故障来源" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="faultFromName" >
              <a-input readOnly v-model="assignDLModel.faultFromName" placeholder="请输入故障来源"  ></a-input>
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="故障处理人" :labelCol="maxLabelCol" :wrapperCol="maxWrapperCol" prop="faultHandler">
              <j-select-user-by-dep store="id" v-model="assignDLModel.faultHandler" ></j-select-user-by-dep>
            </a-form-model-item>
          </a-col>

        </a-row>
      </a-form-model>
    </a-modal>

  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import RentalFaultHandleModal from './modules/RentalFaultHandleModal'
  import { httpAction, getAction, postAction } from '@/api/manage'

  const objTypeOptions = [{
    label: '车辆',
    value: "1"
  }, {
    label: '插座',
    value: "2"
  }, {
    label: '电池柜',
    value: "3"
  }]

  const faultStatusOptions = [{
    label: '待处理',
    value: "0"
  }, {
    label: '处理中',
    value: "1"
  }, {
    label: '已解决',
    value: "2"
  }, {
    label: '已关闭',
    value: "3"
  }]

  const faultFromOptions = [{
    label: '客户上报',  
    value: "1"
  },{
    label: '经营方上报',  
    value: "2"
  }]

  export default {
    name: 'RentalFaultHandleList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      RentalFaultHandleModal
    },
    data () {
      return {
        description: '故障处理管理页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          // {
          //   title:'故障对象id',
          //   align:"center",
          //   dataIndex: 'objId'
          // },
          {
            title:'故障对象',
            align:"center",
            dataIndex: 'objType',
            customRender:function (text) {
              if(text && objTypeOptions.length>0){
                const objType = objTypeOptions.find(item => item.value === text);
                if(objType){
                  return objType.label;
                }
              }
              return text;
            }
          },
          // {
          //   title:'维度',
          //   align:"center",
          //   dataIndex: 'latitude'
          // },
          // {
          //   title:'经度',
          //   align:"center",
          //   dataIndex: 'longitude'
          // },
          {
            title:'故障类型',
            align:"center",
            dataIndex: 'faultCodeName'
          },
          {
            title:'故障描述',
            align:"center",
            dataIndex: 'faultDesc'
          },
          {
            title:'故障状态',
            align:"center",
            dataIndex: 'faultStatus',
            customRender:function (text) {
              if(text && faultStatusOptions.length>0){
                const faultStatus = faultStatusOptions.find(item => item.value === text);
                if(faultStatus){
                  return faultStatus.label;
                }
              }
              return text;
            }
          },
          {
            title:'故障处理人',
            align:"center",
            dataIndex: 'faultHandler'
          },
          {
            title:'解决时间',
            align:"center",
            dataIndex: 'resolvedDate',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'故障上报人',
            align:"center",
            dataIndex: 'faultReporter'
          },
          {
            title:'报告时间',
            align:"center",
            dataIndex: 'reportedDate',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'故障来源',
            align:"center",
            dataIndex: 'faultFrom',
            customRender:function (text) {
              if(text && faultFromOptions.length>0){
                const faultFrom = faultFromOptions.find(item => item.value === text);
                if(faultFrom){
                  return faultFrom.label;
                }
              }
              return text;
            }
          },
          // {
          //   title:'是否删除',
          //   align:"center",
          //   dataIndex: 'isDelete'
          // },
          {
            title: this.$t('common.table.actions'),
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/rental/fault-handle/list",
          delete: "/rental/fault-handle/delete",
          deleteBatch: "/rental/fault-handle/deleteBatch",
          exportXlsUrl: "/rental/fault-handle/exportXls",
          importExcelUrl: "/rental/fault-handle/importExcel",
          assignHandlerUrl: "/rental/fault-handle/assign-handler",
          sendSocketUrl: "/sys/socket/send-biz-user",
        },
        dictOptions:{},
        superFieldList:[],
        assignDLVisible:false,
        labelCol: {
          xs: { span: 24 },
          sm: { span: 4 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        maxLabelCol: {
          xs: { span: 24 },
          sm: { span: 2 },
        },
        maxWrapperCol: {
          xs: { span: 24 },
          sm: { span: 22 },
        },
        assignDLModel:{},
        assignDLRules:{
           objId: [
              { required: true, message: '请输入故障对象id!'},
           ],
        },
      }
    },
    created() {
      console.log(this.$route.query.bizId);
      if(this.$route.query.bizId){
        this.$nextTick(()=>{
          this.queryParam.id = this.$route.query.bizId;
          this.searchQuery();
        })
      }
      this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      initDictConfig(){
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'int',value:'objId',text:'故障对象id'})
        fieldList.push({type:'string',value:'objType',text:'故障对象类别(1-车辆 2-插座 3-电池柜)'})
        fieldList.push({type:'string',value:'latitude',text:'维度'})
        fieldList.push({type:'string',value:'longitude',text:'经度'})
        fieldList.push({type:'string',value:'faultCode',text:'故障编号'})
        fieldList.push({type:'string',value:'faultDesc',text:'故障描述'})
        fieldList.push({type:'string',value:'faultStatus',text:'故障状态 0-待处理 1-处理中 2-已解决 3-已关闭'})
        fieldList.push({type:'string',value:'faultHandler',text:'故障处理人'})
        fieldList.push({type:'date',value:'resolvedDate',text:'解决时间'})
        fieldList.push({type:'string',value:'faultReporter',text:'故障上报人'})
        fieldList.push({type:'date',value:'reportedDate',text:'报告时间'})
        fieldList.push({type:'string',value:'faultFrom',text:'故障来源 1-客户上报 2-日常维护'})
        fieldList.push({type:'string',value:'isDelete',text:'是否删除（0：否、1：是）'})
        this.superFieldList = fieldList
      },
      reminder(record){
        const data = {
          "busId": record.id,
          "busType": "FAULT_HANDLE",
          "title": "故障上报",
          "content": record.faultDesc,
          "toUserId": record.faultHandler,
        };
        postAction(this.url.sendSocketUrl, data).then(res=>{
          this.$message.success(res.message);
        })
      },
      assignHandler(record){
        console.log("指定故障处理人------------------");
        console.log(record);
        this.assignDLModel = record;
        this.assignDLVisible = true;
      },
      handleAssignDLOk(e) {
        // this.assignDLVisible = false;
        console.log("handleAssignDLOk-----------:", this.handleAssignDLOk);
        postAction(this.url.assignHandlerUrl, this.assignDLModel).then(res=>{
          console.log();
          if(res.success){
            this.$message.success(res.message);
            this.assignDLVisible = false;
          }else{
            this.$message.success(res.message);
          }
        })
      },
      handleAssignDLCancel(e) {
        console.log('Clicked cancel button');
        this.assignDLVisible = false;
      },
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>